package com.dowsure.apisaas.util;

import com.dowsure.apisaas.annotation.RequestParamValidated;
import com.dowsure.apisaas.exception.ApiException;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

/**
 * @description: 请求参数验证
 * @author: Dowsure
 * @create: 2022-02-15 10:28
 **/
public class RequestParamValidatedUtils {

    /**
     * TODO 判断字段是否为空
     * author BLW
     * @param object
     * return
     * throws ApiException
     * time 2022/2/15 10:39
     */
    public static void validated(Object object) throws ApiException {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (List.class.equals(field.getType())){
                validated((List) ObjectFieldValue.getFieldValueByName(field.getName(), object));
            } else {
                RequestParamValidated validated = field.getAnnotation(RequestParamValidated.class);
                if (validated != null){
                    String message = validated.msg();
                    Object value = ObjectFieldValue.getFieldValueByName(field.getName(), object);
                    String format = validated.format();

                    boolean required = validated.required();
                    int maxLen = validated.maxLen();
                    if (required){
                         if (value == null || value.toString().isEmpty()){
                            throw new ApiException(field.getName() + " （" + message + "） cannot be empty!");
                        }
                    }
                    if (maxLen > ApiConstants.ZERO){
                        if (value != null){
                            int len = value.toString().length();
                            if (maxLen < len){
                                throw new ApiException(field.getName() + "'s value length greater than " + maxLen + "!");
                            }
                        }
                    }

                    if (format != null && !format.trim().isEmpty()){
                        if (value != null){
                            if (!DowsureDateUtils.validationFormat(value.toString(), format)){
                                throw new ApiException(field.getName() + "'s value Date format is not '" + format + "' !");
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * TODO 判断列表字段是否为空
     * author BLW
     * @param list
     * return
     * throws ApiException
     * time 2022/2/15 10:39
     */
    public static void validated(List list) throws ApiException {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            validated(iterator.next());
        }
    }

    public static void notNull(Object obj, String message) throws ApiException {
        if (obj == null){
            throw new ApiException(message + " is null!");
        }
    }

    public static void notBlank(Object obj, String message) throws ApiException {
        if (obj == null){
            throw new ApiException(message + " is null!");
        }
    }

    public static void notBlank(String str, String message) throws ApiException {
        if (str == null || str.isEmpty()){
            throw new ApiException(message + " is null!");
        }
    }

    public static void notBlank(Integer integer, String message) throws ApiException {
        if (integer == null || integer <= 0){
            throw new ApiException(message + " is null or less than or equal to zero!");
        }
    }

    public static void notBlank(BigDecimal decimal, String message) throws ApiException {
        if (decimal == null || decimal.compareTo(new BigDecimal(0)) <= 0){
            throw new ApiException(message + " is null or less than or equal to zero!");
        }
    }
}
